package com.likan.chqjtgc.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.likan.chqjtgc.SecurityUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.joda.time.DateTime;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * @author : liuxinghong
 * @Description :   mybatis-plus 字段填充handler处理器
 * @date: 2020/12/28 19:36
 **/
@Component
public class MyMetaObjectHandler  implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        //创建ID
        this.strictInsertFill(metaObject, "id", Long.class, IdWorker.getId());
        //创建时间填充
        this.strictInsertFill(metaObject, "createTime", Date.class, DateTime.now().toDate());
        //创建人填充
        this.strictInsertFill(metaObject, "createUserId",Long.class, SecurityUtils.getCurrentUserId());
        //未删除填充
        this.strictInsertFill(metaObject, "isDelete",Integer.class, 1);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        //更新时间填充
        this.strictUpdateFill(metaObject, "updateTime", Date.class, DateTime.now().toDate());
    }
}
